<?php
namespace Home\Controller;
use Think\Controller;
class FieldsController extends CommonController {
    /*
     * 数据库 字段 控制器
     */
    public function index(){

    	if(IS_GET){

    		$map['mid'] = session('mid');

    		$DB = M('field');
    		/**
			数据分页显示开始
			**/
			$count      = $DB->where($map)->count();// 查询满足要求的总记录数
			$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
			$Page->lastSuffix=false;
                        $Page->setConfig('header','<li class="rows">共<b>%TOTAL_ROW%</b>条记录&nbsp;&nbsp;每页<b>20</b>条&nbsp;&nbsp;第<b>%NOW_PAGE%</b>页/共<b>%TOTAL_PAGE%</b>页</li>');
                        $Page->setConfig('prev','上一页');
                        $Page->setConfig('next','下一页');
                        $Page->setConfig('last','末页');
                        $Page->setConfig('first','首页');
                        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $DB->where($map)->order('rank asc')->limit($Page->firstRow.','.$Page->listRows)->select();
			/**
			数据分页结束
			**/
    		// $list = $DB->where($map)->select();
    		// print_r($list); die;
    		$this->assign('show',$show);
    		$this->assign('list',$list);
    		$this->display();
    	}
    }

    /**
     * 添加新的系统字段
     */
    public function add(){

    	if(IS_GET){


    		$this->display();
    	}else if(IS_POST){

    		$data['fl_name'] = trim(I('post.fl_name'));
    		$data['fl_name_ch'] = trim(I('post.fl_name_ch'));
    		$type_name = trim(I('post.type_name'));
    		if(!empty($type_name)){

    			$data['Type_name'] = $type_name;
    		}
    		if($data['Type_name'] == 'CHAR' || $data['Type_name'] == 'VARCHAR'){

    			$data['Lengths'] = trim(I('post.lengths'));
    		}
    		// $data['Index'] = trim(I('post.index'));
    		$index_type = trim(I('post.index'));
    		if(!empty($index_type)){

    			$data['Index'] = $index_type;
    		}
    		if($data['Index'] == 'PRIMARY KEY'){

    			$data['isKey'] = 1;
    			$data['Extra_value'] = 1;
    		}else{

    			$data['isKey'] = 0;
    			$data['Extra_value'] = 0;
    		}

    		$data['isNull'] = I('post.isnull');
    		if($data['isNull'] == 1){

    			$data['Default_value'] = NULL;
    		}else{

    			$data['Default_value'] = trim(I('post.default_value'));
    		}

    		$data['isUnsigned'] = I('post.isunsigned');
    		$data['comment'] = trim(I('post.comment'));
    		$data['rank'] = trim(I('post.rank'));
    		$data['mid'] = session('mid');
    		$data['ctime'] = I('server.REQUEST_TIME');
    		// print_r($data); die;
    		$DB = M('field');

    		$re = $DB->data($data)->add();

    		if(!empty($re)){

    			$this->success('添加成功!',U('Home/Fields/index'),1);
    		}else{

    			$this->error('添加失败!','',1);
    		}
    	}
    }


    /**
     * ajax删除字段
     */
    public function del_field(){

    	if(IS_AJAX){

    		$map['flid'] = I('post.flid');
    		$map['mid'] = session('mid');
    		// print_r($map); die;
    		$DB = M('field');

    		$re = $DB->where($map)->delete();
    		if($re){
    			echo json_encode(1);
    		}else{
    			echo json_encode(0);
    		}
    	}
    }


    /**
     * 修改字段
     */
    public function modify(){

    	if(IS_GET){

    		$map['flid'] = I('get.flid');
    		$map['mid'] = session('mid');

    		$DB = M('field');

    		$list = $DB->where($map)->find();

    		$this->assign('list',$list);
    		$this->display();
    	}else{

    		$data['fl_name'] = trim(I('post.fl_name'));
    		$data['fl_name_ch'] = trim(I('post.fl_name_ch'));
    		$type_name = trim(I('post.type_name'));
    		if(!empty($type_name)){

    			$data['Type_name'] = $type_name;
    		}
    		if($data['Type_name'] == 'CHAR' || $data['Type_name'] == 'VARCHAR'){

    			$data['Lengths'] = trim(I('post.lengths'));
    		}
    		// $data['Index'] = trim(I('post.index'));
    		$index_type = trim(I('post.index'));
    		if(!empty($index_type)){

    			$data['Index'] = $index_type;
    		}
    		if($data['Index'] == 'PRIMARY KEY'){

    			$data['isKey'] = 1;
    			$data['Extra_value'] = 1;
    		}else{

    			$data['isKey'] = 0;
    			$data['Extra_value'] = 0;
    		}

    		$data['isNull'] = I('post.isnull');
    		if($data['isNull'] == 1){

    			$data['Default_value'] = NULL;
    		}else{

    			$data['Default_value'] = trim(I('post.default_value'));
    		}

    		$data['isUnsigned'] = I('post.isunsigned');
    		$data['comment'] = trim(I('post.comment'));
    		$data['rank'] = trim(I('post.rank'));
    		// $data['mid'] = session('mid');
    		// print_r($data); die;
    		$DB = M('field');

    		$map['flid'] = I('post.flid');
    		$map['mid'] = session('mid');
    		$re = $DB->where($map)->data($data)->save();

    		if($re){

    			$this->success('修改成功!',U('Home/Fields/index'),1);
    		}else{

    			$this->error('修改失败!','',1);
    		}
    	}
    }
}
